<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>Fluenty Survey</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
      }
      body {
        background-color: #fff;
        color: #0f4c81;
        display: flex;
        min-height: 100vh;
        flex-direction: column;
        max-width: 600px;
        margin: 0 auto;
      }
      .container {
        padding: 40px 20px;
        display: flex;
        flex-direction: column;
        align-items: center;
        flex: 1;
        justify-content: flex-start;
      }

      /* Header and Navigation */
      .header {
        width: 100%;
        display: flex;
        align-items: center;
        margin-bottom: 60px;
        position: relative;
      }
      .back-button {
        position: absolute;
        left: 0;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: none;
        border: none;
        cursor: pointer;
        color: #555;
      }
      .progress-bar {
        width: 100%;
        height: 8px;
        background-color: #e0e0e0;
        border-radius: 4px;
        overflow: hidden;
        margin-left: 50px;
      }
      .progress-fill {
        height: 100%;
        width: 97%;
        background-color: #4991ef;
        border-radius: 4px;
      }

      /* Content */
      .content {
        width: 100%;
        padding: 20px 0;
        display: flex;
        flex-direction: column;
        align-items: center;
      }
      .title {
        font-size: 32px;
        font-weight: 700;
        color: #0f4c81;
        margin-bottom: 40px;
        text-align: center;
        line-height: 1.3;
      }

      /* Name Input */
      .name-input {
        width: 100%;
        padding: 20px;
        border: 1px solid #e0e0e0;
        border-radius: 24px;
        font-size: 18px;
        color: #555;
        outline: none;
        transition: border-color 0.2s ease;
      }
      .name-input:focus {
        border-color: #4991ef;
      }
      .name-input::placeholder {
        color: #999;
      }

      /* Continue Button */
      .continue-button {
        width: 100%;
        padding: 18px;
        background-color: #a5cefb;
        color: white;
        border: none;
        border-radius: 16px;
        font-size: 18px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
        position: fixed;
        bottom: 20px;
        max-width: 560px;
      }
      .continue-button.active {
        background-color: #4991ef;
      }
      .continue-button:disabled {
        opacity: 0.7;
        cursor: not-allowed;
      }

      @media (max-width: 480px) {
        .title {
          font-size: 28px;
        }
        .name-input {
          font-size: 16px;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <!-- Header with back button and progress bar -->
      <div class="header">
        <button class="back-button">
          <span class="material-icons">arrow_back</span>
        </button>
        <div class="progress-bar">
          <div class="progress-fill"></div>
        </div>
      </div>

      <!-- Main content -->
      <div class="content">
        <h1 class="title">What's your name?</h1>

        <!-- Name input field -->
        <input type="text" class="name-input" placeholder="Enter your name" id="nameInput" />
      </div>

      <!-- Continue button (initially disabled) -->
      <button class="continue-button" id="continueButton" disabled>Continue</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const backButton = document.querySelector('.back-button');
            const nameInput = document.getElementById('nameInput');
            const continueButton = document.getElementById('continueButton');

            // 从 localStorage 中获取之前保存的姓名
            const storedName = localStorage.getItem('userName');
            if (storedName) {
                nameInput.value = storedName;
                const hasValue = nameInput.value.trim()!== '';
                continueButton.disabled =!hasValue;
                if (hasValue) {
                    continueButton.classList.add('active');
                } else {
                    continueButton.classList.remove('active');
                }
            }

            // 页面加载时检查输入框是否有内容
            const hasValue = nameInput.value.trim()!== '';
            continueButton.disabled =!hasValue;
            if (hasValue) {
                continueButton.classList.add('active');
            } else {
                continueButton.classList.remove('active');
            }

            // Back button handler
            backButton.addEventListener('click', function () {
                window.history.back();
            });

            // Input change handler to enable/disable continue button
            nameInput.addEventListener('input', function () {
                const hasValue = nameInput.value.trim()!== '';
                continueButton.disabled =!hasValue;

                if (hasValue) {
                    continueButton.classList.add('active');
                } else {
                    continueButton.classList.remove('active');
                }
            });

            // Continue button handler
            continueButton.addEventListener('click', function () {
                if (nameInput.value.trim()!== '') {
                    // 获取页面标题和当前页面名称
                    const pageTitle = document.querySelector('.title')
                       ? document.querySelector('.title').textContent.trim()
                        : "What's your name?";
                    const pageName = window.location.pathname.split('/').pop().split('.')[0];
                    const userName = nameInput.value.trim();

                    // 创建数据对象
                    const data = {
                        title: pageTitle,
                        value: userName,
                        name: userName,
                    };

                    // 以 JSON 格式存储到 localStorage
                    localStorage.setItem(pageName, JSON.stringify(data));

                    // 保留原有存储方式，以保证兼容性
                    localStorage.setItem('userName', userName);

                    // Navigate to next page
                    window.location.href = 'yourReady.html';
                }
            });

            // Allow Enter key to submit
            nameInput.addEventListener('keypress', function (e) {
                if (e.key === 'Enter' && nameInput.value.trim()!== '') {
                    continueButton.click();
                }
            });
        });
    </script>
  </body>
</html>
